package com.smart.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smart.common.dto.product.CategoryDto;
import com.smart.common.utils.CommonBeanUtils;
import com.smart.web.api.CategoryApi;
import com.smart.web.entity.Banner;
import com.smart.web.entity.Navigation;
import com.smart.web.entity.Recommend;
import com.smart.web.mapper.BannerMapper;
import com.smart.web.mapper.NavigationMapper;
import com.smart.web.mapper.RecommendMapper;
import com.smart.web.service.HomeService;
import com.smart.web.vo.BannerVo;
import com.smart.web.vo.HomeVo;
import com.smart.web.vo.NavigationVo;
import com.smart.web.vo.RecommendVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangwei
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private BannerMapper bannerMapper;
    @Resource
    private NavigationMapper navigationMapper;
    @Resource
    CategoryApi categoryApi;
    @Resource
    private RecommendMapper recommendMapper;

    /**
     *
     */
    @Override
    public HomeVo getHeadData() {
        HomeVo homeVo = new HomeVo();
        /**
         * 获取轮播数据
         */
        List<Banner> banners = bannerMapper.selectList();
        //  将po对象转化成vo对象的集合
        List<BannerVo> bannerList = CommonBeanUtils.copyListProperties(banners, BannerVo::new);
        homeVo.setBanners(bannerList);
        /**
         * 获取导航数据
         */
        LambdaQueryWrapper<Navigation> lambda = new LambdaQueryWrapper<>();
        lambda.eq(Navigation::getStatus, 1);
        List<NavigationVo> navigationVos = CommonBeanUtils.copyListProperties(navigationMapper.selectList(lambda), NavigationVo::new);
        homeVo.setNavigations(navigationVos);
        /**
         * 获取分类信息
         */
        List<CategoryDto> categories = categoryApi.list();
        homeVo.setCategories(categories);
        return homeVo;
    }

    @Override
    public HomeVo getContentData() {
        HomeVo homeVo = new HomeVo();
        // 获取推荐的活动的相关商品信息
        QueryWrapper<Recommend> qw = new QueryWrapper<>();
        qw.lambda().eq(Recommend::getIsShow, 1).eq(Recommend::getStatus, 1);
        List<Recommend> recommends = recommendMapper.selectList(qw);
        homeVo.setRecommends(CommonBeanUtils.copyListProperties(recommends, RecommendVo::new));
        return homeVo;
    }
}
